<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<link rel="stylesheet" href="static/assets/jstree/style.min.css"/>
<div class="modal fade" id="ocean-modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog" style="width: 1000px; height: auto">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <c:choose>
                    <c:when test="${entity.id != null && entity.id!= ''}">
                        <h4 class="modal-title">编辑菜单</h4>
                    </c:when>
                    <c:otherwise>
                        <h4 class="modal-title">新增菜单</h4>
                    </c:otherwise>
                </c:choose>
            </div>
            <div class="modal-body">
                <div class=" form">
                    <form id="user_form" class="form-horizontal" method="post" data-url="${url}" data-class="">
                        <div class="col-lg-12" style="">
                            <div class="col-lg-6">
                                <input type="hidden" id="entity_id" name="id" value="${entity.id}">
                                <div class="form-group">
                                    <label for="username" class="control-label col-lg-2">用户名 </label>
                                    <div class="col-lg-10">
                                        <input class="form-control" id="username" name="username" type="text"
                                               placeholder="用户名"
                                               value="${entity.username}"
                                               required/>
                                    </div>
                                </div>
                                <c:if test="${entity.id == null || entity.id == ''}">
                                    <div class="form-group">
                                        <label for="password" class="control-label col-lg-2">密码 </label>
                                        <div class="col-lg-10">
                                            <input class="form-control" id="password" name="password" type="password"
                                                   placeholder="密码"
                                                   value="" required/>
                                        </div>
                                    </div>
                                </c:if>
                                <div class="form-group">
                                    <label for="realName" class="control-label col-lg-2">姓名 </label>
                                    <div class="col-lg-10">
                                        <input class="form-control" id="realName" name="realName" type="text"
                                               placeholder="姓名"
                                               value="${entity.realName}"
                                               required/>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="phone" class="control-label col-lg-2">电话 </label>
                                    <div class="col-lg-10">
                                        <input class="form-control" id="phone" name="phone" type="number"
                                               placeholder="电话"
                                               value="${entity.phone}" required/>
                                    </div>
                                </div>

                                <div class="form-group">
                                    <label class="control-label col-lg-2">角色</label>
                                    <div class="col-lg-10">
                                        <c:forEach items="${roles}" var="role">
                                            <label class="checkbox">
                                                <input id="${role.id}" name="role" type="checkbox"
                                                       value="${role.id}"
                                                       required> ${role.name}
                                            </label>
                                        </c:forEach>
                                    </div>
                                </div>
                                <input type="hidden" name="group" id="group" value="${group}" required/></div>
                            <div class="col-lg-6">
                                <div class="row-fluid">
                                    <div class="span12">
                                        <div class="control-group">
                                            <label class="control-label" for="group">区域 </label>
                                        </div>
                                    </div>
                                </div>
                                <div class="row-fluid">
                                    <div class="span6">
                                        <div id="groupTree" class="demo"></div>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-lg-offset-2 col-lg-10">
                                <button class="btn btn-success" type="button" onclick="formSubmit(validator)">
                                    Save
                                </button>
                                <button class="btn btn-default" type="button" data-dismiss="modal">Cancel
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<c:forEach items="${entity.roles}" var="uRole">
    <input type="hidden" name="user_role" value="${uRole.id}"/>
</c:forEach>
<script type="application/javascript" src="static/assets/jstree/jstree.min.js"></script>
<script type="application/javascript">
    $('#groupTree').jstree({
        'core': {
            'data': {
                "url": "resources/group/treeData",
                "dataType": "json"
            }
        }
    }).on('changed.jstree', function (e, data) {
        console.log(data);
        if (data.action == 'select_node') {
            $("#group").val(data.node.id);
        }
    }).on('loaded.jstree', function (e, data) {
        $('#groupTree').jstree(true).select_node($("#group").val());
    });

    $.each($("input[name='user_role']"), function (index, el) {
        console.log(el);
        $("#" + $(el).val()).attr("checked", true);
    });

    var validator = $("#user_form").validate();
</script>

